package com.vitabuybuy.tv.ui.mine.group.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.GroupBuyOrderListDto
import com.vitabuybuy.tv.dto.MyGroupBuyDetailDto
import com.vitabuybuy.tv.network.GroupBuyRepository

class MyGroupVieModel : BaseViewModel() {

    val goodslist = MutableLiveData<MutableList<GroupBuyOrderListDto>>()

    fun getOrderList(groupType: String?, status: String, pageNumber: Int) {
        val map = HashMap<String, Any>()
        if(groupType == null){
            map["status"] = status
        }else{
            map["purchasingGroup"] = mapOf(Pair("type", groupType), Pair("status", status))
        }
        rxHttpRequest {
            onRequest = {
                val it = GroupBuyRepository.getMyGroupBuyOrderList(groupType,map, pageNumber) .await()
                goodslist.value = it
                defUI.msgEvent.postValue(MessageModel(1, arg1 = it.size))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isLoadingType = true
            isRefresh = pageNumber == 1
        }
    }


    val groupDetail = MutableLiveData<MyGroupBuyDetailDto>()

    fun getMyGroupBuyDetail(id: Int?, type: Int?) {
        rxHttpRequest {
            onRequest = {
                val it = GroupBuyRepository.getMyGroupBuyDetail(id, type)  .await()
                groupDetail.value = it
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }
}
